package gd.qaq.blue.bluesname.commands;

import gd.qaq.blue.bluesname.BluesName;
import gd.qaq.blue.bluesname.prompt.ChangeNamePrompt;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.Conversable;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.conversations.Prompt;
import org.bukkit.entity.Player;

import static gd.qaq.blue.bluesname.BluesName.*;

/**
 * 反编译的同学~好好学着点噢
 * 作者:blue qq:862899738
 */
public class BluesNameCommands implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            switch (args.length) {
                case 0:
                    help((Player) sender);
                    return true;
                case 1:
                    if (sender.isOp() && args[0].equalsIgnoreCase("reload")) {
                        BluesName.plugin.reload((Player) sender);
                        return true;
                    }
                    if (args[0].equalsIgnoreCase("prefix") || args[0].equalsIgnoreCase("suffix")) {
                        if (!sender.isOp() && !sender.hasPermission("bluesname.*")) {
                            break;
                        }
                        ChangeNamePrompt prompt = new ChangeNamePrompt(args[0].equalsIgnoreCase("prefix"), (Player) sender);
                        Conversation conversation = new ConversationFactory(plugin).withFirstPrompt((Prompt) prompt).buildConversation((Conversable) sender);
                        conversation.begin();
                        return true;
                    }
            }
        }
        error(sender, "参数有误!");
        return false;
    }

    private void help(Player player) {
        if (!player.isOp() && !player.hasPermission("bluesname.*")) {
            return;
        }
        StringBuilder helpContent = new StringBuilder();
        helpContent.append("&6&l-----------&b&lBluesName&6&l------------\n");
        helpContent.append("&b(插件指令可以简化为bname,bn)\n");
        helpContent.append("&b/bluesname prefix &e自定义前缀\n");
        helpContent.append("&b/bluesname suffix &e自定义后缀\n");
        if (player.isOp()) {
            helpContent.append("&b/bluesname reload &e重载插件\n");
        }
        helpContent.append("&6&l--------------------------------");
        player.sendMessage(colorFormat(helpContent.toString()));
    }
}
